/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 玩家加载模块
	/// module : loadPlayer
	/// </summary>
	public interface SNetLoadPlayer : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.loadPlayer_getRoleList, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.loadPlayer_getRoleList, "Recv->" + data.ToString());
#endif
		                GetRoleListRequest _data = (GetRoleListRequest)data;
						return CS_getRoleList(session);
		            } 
            	},
                { NetMessageId.loadPlayer_chooseRole, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.loadPlayer_chooseRole, "Recv->" + data.ToString());
#endif
		                ChooseRoleRequest _data = (ChooseRoleRequest)data;
						return CS_chooseRole(session, _data.pid);
		            } 
            	},
                { NetMessageId.loadPlayer_createRole, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.loadPlayer_createRole, "Recv->" + data.ToString());
#endif
		                CreateRoleRequest _data = (CreateRoleRequest)data;
						return CS_createRole(session, _data.roleCid, _data.name);
		            } 
            	},
                { NetMessageId.loadPlayer_loadPlayer, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.loadPlayer_loadPlayer, "Recv->" + data.ToString());
#endif
		                LoadPlayerRequest _data = (LoadPlayerRequest)data;
						return CS_loadPlayer(session);
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.loadPlayer_getRoleList, ()=> new GetRoleListRequest()},
	        	{NetMessageId.loadPlayer_chooseRole, ()=> new ChooseRoleRequest()},
	        	{NetMessageId.loadPlayer_createRole, ()=> new CreateRoleRequest()},
	        	{NetMessageId.loadPlayer_loadPlayer, ()=> new LoadPlayerRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        /// 获取角色列表
        /// </summary>
	    protected GetRoleListResult CS_getRoleList(ISession session);
		/// <summary>
        /// 选择角色进入游戏
        /// </summary>
	    protected ChooseRoleResult CS_chooseRole(ISession session, long pid);
		/// <summary>
        /// 创建角色
        /// </summary>
	    protected CreateRoleResult CS_createRole(ISession session, int roleCid, string name);
		/// <summary>
        /// 加载玩家信息
        /// </summary>
	    protected LoadPlayerResult CS_loadPlayer(ISession session);
#endregion
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
        public GetRoleListResult SC_getRoleListResult(Task<GetRoleListResult> future){return new GetRoleListResult(future);}
        public GetRoleListResult SC_getRoleListResult(int code, List<RoleInfoPOD> roleList){return new GetRoleListResult(code, roleList);}
        public GetRoleListResult SC_getRoleListResult(int code){return new GetRoleListResult(code);}
		/// <summary>
        /// 
        /// </summary>
        public ChooseRoleResult SC_chooseRoleResult(Task<ChooseRoleResult> future){return new ChooseRoleResult(future);}
        public ChooseRoleResult SC_chooseRoleResult(int code, string message){return new ChooseRoleResult(code, message);}
        public ChooseRoleResult SC_chooseRoleResult(int code){return new ChooseRoleResult(code);}
		/// <summary>
        /// 
        /// </summary>
        public CreateRoleResult SC_createRoleResult(Task<CreateRoleResult> future){return new CreateRoleResult(future);}
        public CreateRoleResult SC_createRoleResult(int code, RoleInfoPOD role){return new CreateRoleResult(code, role);}
        public CreateRoleResult SC_createRoleResult(int code){return new CreateRoleResult(code);}
		/// <summary>
        /// 
        /// </summary>
        public LoadPlayerResult SC_loadPlayerResult(Task<LoadPlayerResult> future){return new LoadPlayerResult(future);}
        public LoadPlayerResult SC_loadPlayerResult(int code, PlayerPOD player){return new LoadPlayerResult(code, player);}
        public LoadPlayerResult SC_loadPlayerResult(int code){return new LoadPlayerResult(code);}
#endregion

#region Messages
	public class GetRoleListResult : MessageResult {
	    private List<RoleInfoPOD> roleList;
	    public GetRoleListResult(Task<GetRoleListResult> future): base(future) { }
		public GetRoleListResult(int code): base(code) { }
		public new GetRoleListResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal GetRoleListResult(int code, List<RoleInfoPOD> roleList){
	        this.code = code;
	        this.roleList = roleList == null ? null : new (roleList);
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<GetRoleListResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.loadPlayer_getRoleListResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.loadPlayer_getRoleListResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_list_pod(roleList);
			return si_p;
		}
		public override string ToString() {
    		return "LoadPlayer.SC_getRoleListResult { "
			+ "code="
			+ code
			+ ", roleList="
			+ roleList.ToString<RoleInfoPOD>()
			+" }";
		}
	}
	public class ChooseRoleResult : MessageResult {
	    private string message;
	    public ChooseRoleResult(Task<ChooseRoleResult> future): base(future) { }
		public ChooseRoleResult(int code): base(code) { }
		public new ChooseRoleResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal ChooseRoleResult(int code, string message){
	        this.code = code;
	        this.message = message;
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<ChooseRoleResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.loadPlayer_chooseRoleResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.loadPlayer_chooseRoleResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_string(message);
			return si_p;
		}
		public override string ToString() {
    		return "LoadPlayer.SC_chooseRoleResult { "
			+ "code="
			+ code
			+ ", message="
			+ message
			+" }";
		}
	}
	public class CreateRoleResult : MessageResult {
	    private RoleInfoPOD role;
	    public CreateRoleResult(Task<CreateRoleResult> future): base(future) { }
		public CreateRoleResult(int code): base(code) { }
		public new CreateRoleResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal CreateRoleResult(int code, RoleInfoPOD role){
	        this.code = code;
	        this.role = role;
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<CreateRoleResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.loadPlayer_createRoleResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.loadPlayer_createRoleResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_pod(role);
			return si_p;
		}
		public override string ToString() {
    		return "LoadPlayer.SC_createRoleResult { "
			+ "code="
			+ code
			+ ", role="
			+ role
			+" }";
		}
	}
	public class LoadPlayerResult : MessageResult {
	    private PlayerPOD player;
	    public LoadPlayerResult(Task<LoadPlayerResult> future): base(future) { }
		public LoadPlayerResult(int code): base(code) { }
		public new LoadPlayerResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal LoadPlayerResult(int code, PlayerPOD player){
	        this.code = code;
	        this.player = player;
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<LoadPlayerResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.loadPlayer_loadPlayerResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.loadPlayer_loadPlayerResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_pod(player);
			return si_p;
		}
		public override string ToString() {
    		return "LoadPlayer.SC_loadPlayerResult { "
			+ "code="
			+ code
			+ ", player="
			+ player
			+" }";
		}
	}
	private class GetRoleListRequest : IReceiveMessage
    {
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
		}
        public int GetMessageId() { return NetMessageId.loadPlayer_getRoleList; }
        public override string ToString()
        {
            return "LoadPlayer.getRoleList";
        }
    }
	private class ChooseRoleRequest : IReceiveMessage
    {
		internal long pid;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            pid = _in.HasRemaining() ? _in.Read_long() : default;
		}
        public int GetMessageId() { return NetMessageId.loadPlayer_chooseRole; }
        public override string ToString()
        {
            return "LoadPlayer.chooseRole"+ ", pid="+ pid;
        }
    }
	private class CreateRoleRequest : IReceiveMessage
    {
		internal int roleCid;
		internal string name;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            roleCid = _in.HasRemaining() ? _in.Read_int() : default;
            name = _in.HasRemaining() ? _in.Read_string() : default;
		}
        public int GetMessageId() { return NetMessageId.loadPlayer_createRole; }
        public override string ToString()
        {
            return "LoadPlayer.createRole"+ ", roleCid="+ roleCid+ ", name="+ name;
        }
    }
	private class LoadPlayerRequest : IReceiveMessage
    {
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
		}
        public int GetMessageId() { return NetMessageId.loadPlayer_loadPlayer; }
        public override string ToString()
        {
            return "LoadPlayer.loadPlayer";
        }
    }
	}
#endregion
}